package com.mcp.converter.domain.rule.compatibility.function;

import com.mcp.converter.domain.checker.IncompatibilityDetail;
import com.mcp.converter.shared.response.sql.SQLElement;
import com.mcp.converter.domain.rule.compatibility.AbstractFunctionRule;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * FIRST_VALUE/LAST_VALUE函数兼容性规则
 * OceanBase与Oracle在默认窗口范围处理上存在差异
 * @author zengxiong
 */
public class FirstLastValueFunctionRule extends AbstractFunctionRule {

    private static final String RULE_ID = "FN004";
    private static final String RULE_NAME = "FIRST_VALUE/LAST_VALUE函数部分兼容";
    private static final String RULE_DESCRIPTION = "OceanBase与Oracle在FIRST_VALUE/LAST_VALUE函数的默认窗口范围处理上存在差异";

    public FirstLastValueFunctionRule() {
        super(RULE_ID, RULE_NAME, RULE_DESCRIPTION);
        setIncompatibilityDetail(
                "OceanBase与Oracle在FIRST_VALUE/LAST_VALUE函数的默认窗口范围处理上存在差异。",
                "默认情况下，Oracle和OceanBase对窗口函数的帧（ROWS/RANGE BETWEEN ...）定义可能不同，导致结果不一致。",
                "建议显式指定窗口函数的窗口范围 (e.g., ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) 以确保行为一致。",
                IncompatibilityDetail.Severity.MEDIUM
        );
    }

    @Override
    public boolean matches(Object target) {
        if (target instanceof SQLElement) {
            SQLElement element = (SQLElement) target;
            if (isFunctionElement(element)) {
                String functionName = element.getName().toUpperCase();
                return "FIRST_VALUE".equals(functionName) || "LAST_VALUE".equals(functionName);
            }
        }
        return false;
    }
    
    @Override
    public Set<String> getIncompatibleFunctions() {
        // 这些函数在OceanBase中是支持的，但存在实现差异，不应标记为完全不兼容
        return new HashSet<>();
    }
}
